* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"


* -------------------------------------------------------------------------------------------------
* Prepare data for figures
* -------------------------------------------------------------------------------------------------

* DI entrant outcomes, by x, where x = 
*   i.covstart_year          = calendar year of Medicare entry
*   c.unemprateatapp         = unemployment rate at time of application
*   i.unemp_q20              = ventiles of unemprateatapp
*   i.age_year_covstart_fill = age in years at Medicare entry

* Sample
*   main: individuals who enter Medicare at ages 20-62, and annual Medicare spending after first (usually partial) year of Medicare coverage
*   2262: main, but individuals who enter Medicare at ages 22-62
local sample 2262

* Sample definitions:
* - main (all ages, all years)
local sample_main 1
* - ages 22-62
local sample_2262 inrange(age_year_covstart_fill, 22, 62)
* - ages 51-52
local sample_5152 inrange(age_year_covstart_fill, 51, 52)
* - first full year
local sample_ffy (rfrnc_yr == init_yr + 1)

* Which controls were included in each regression
local controls_01 i.age_year_covstart_fill
local controls_02 i.fipscounty_firstnm_g##i.age_year_covstart_fill
local controls_03 i.covstart_month##i.age_year_covstart_fill

* Estimation
local y incidence_pop_age_atapp
local x c.unemp_rate_county_atapp
local clust county_mofd

* DI entrant cohort size, average Medicare spending, and mortality
foreach ctrl in 01 02 03 {
  * local ctrl 02
  
  * Confirm sample
  assert "`sample'" == "2262"
  
  * Load estimates of cyclical entry by age
  estimates use "$SSDIMed/results/estimates/x-`x'/x-`x'-age_year_covstart_fill_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'.ster"
  regsave, ci detail(all)
  
  * which observations and variables to keep
  drop if var == "_cons"
  keep var coef stderr ci_lower ci_upper absvars
  
  * numeric level for the age
  gen age = substr(var, 1, strpos(var, "."))
  replace age = regexr(age, "(b\.)|(\.)", "")
  destring age, replace
  order age, after(var)
  
  * Specification
  assert absvars == "`controls_`ctrl''"
  gen spec = `ctrl'
  order spec
  
  * Save
  list, sep(0)
  tempfile results_`ctrl'
  save `results_`ctrl''
}

clear
foreach ctrl in 01 02 03 {
  append using `results_`ctrl''
}
list, sepby(spec)


* ---------------------------------------------------------------------------------------
* Graph settings
* ---------------------------------------------------------------------------------------

* Set graph style to project default settings
*	Pass as arguments: height width (default 3.5in 6.5in)
do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do" 3.6in 6.5in

grstyle set size 10.5pt: axis_title

local red "179 0 0"
local green "75 115 47"
local blue "48 84 150"
local bluegray "126 153 180"
local brown "140 045 004"
local orange "237 125 49"
local tan "210 180 140"

* Spec 01: no FEs interacted with age
local p 1
local blue 68 114 196
grstyle set color `blue': p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern longdash: p`p'
grstyle set linewidth 1.5pt: p`p'

* Spec 02: county FEs interacted with age (baseline)
local p 2
local color`p' `brown'
grstyle set color "`color`p''": p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.8pt: p`p'
* shaded region for confidence intervals
grstyle set color "`color`p''%20": p`p'area
grstyle set color "`color`p''%20": p`p'arealine
grstyle set linewidth vvthin: p`p'area

* Spec 03: covstart_month FEs interacted with age
local p 3
grstyle set color black: p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.5pt: p`p'

* Medical spending
local p 4
grstyle set color black: p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.8pt: p`p'

* Arrows and lines
local p 5
grstyle set color gs8: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth vvthin: p`p'


* -------------------------------------------------------------------------------------------------
* Figure: Cyclicality of DI entry by age at entry
* -------------------------------------------------------------------------------------------------

* Describbe data
list, sep(0)
assert inrange(age, 22, 62)

* xvar is age at entry, taken to be two years before Medicare coverage starts
cap drop xvar
gen double xvar = age - 2
assert inrange(floor(xvar), 22 - 2, 62 - 2)
qui glevelsof xvar
assert r(J) == (62 - 22 + 1)



if 1 {

* Make min and max labels align on both y axes
* Stata tip 93: Handling multiple y axes on twoway graphs: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1101000411

* How many major ticks?
* How much should range extend beyond ticks (as fraction of tick step size)
local y_ticks 9
local y_margin_lo 0
local y_margin_hi 0.4

* y1 tick lo, step size, format
local y1_tick_lo 0
local y1_step 10
local y1_fmt "%9.2g"

* Construct y labels and scales from parameters above
forvalues y = 1/1 {
  local y`y'_tick_hi  = `y`y'_tick_lo' + `y`y'_step' * (`y_ticks' - 1)
  local y`y'_range_lo = `y`y'_tick_lo' - `y`y'_step' * `y_margin_lo'
  local y`y'_range_hi = `y`y'_tick_hi' + `y`y'_step' * `y_margin_hi'
  di "y`y'_tick_hi:  `y`y'_tick_hi'"
  di "y`y'_range_lo: `y`y'_range_lo'"
  di "y`y'_range_hi: `y`y'_range_hi'"
  
  local y`y'_label ylabel(`y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi', axis(`y') format("`y`y'_fmt'") noticks nolabels labgap(1pt)) 
  local y`y'_scale yscale(`yalt' range(`y`y'_range_lo' `y`y'_range_hi') axis(`y') noline) 
  di `"y`y'_label:  `y`y'_label'"'
  di `"y`y'_scale:  `y`y'_scale'"'
}

* x-axis settings
local xlo 18
local xhi 61
local xscale xscale(range(`xlo' `xhi'))
local x_axis xlabel(20(5)62) `xscale' xtitle("Age at entry", margin(t=+.5))

* style and color for y-axis title/labels
local y1_title_style it
local y1_title_color gs8
local y1_label_style it
local y1_label_color gs8
local y1_label_size 9pt

local text_style sf
local text_color black

* y-axis labels placed above grid rule (build manually)
local y1_x `xlo'
local y1_place ne
local y1_just left

local y 1
local y`y'_la
foreach tick of numlist  `y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi' {
  if `tick' == `y`y'_tick_hi' {
    * white textbox to go behind label
    local y`y'_la `y`y'_la' text(`=`tick' + `y`y'_step'/100' `y`y'_x' " ", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') width(5) height(3) bcolor(white%70))
    *label
    local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'`y`y'_unit'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
  }
  else {
    local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
  }
}

* y-axis titles, horizontal orientation (build manually)
local y 1
local y1title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xlo' "{`y`y'_title_style':Entrants per million residents}"
local y1title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') bcolor(white) margin(b=+.5))
local y1title `y`y'title' ytitle(" ", axis(`y') margin(l=-8))

* y-axis settings
local y_axis1 `y1_label' `y1_la' `y1_scale' `y1title'

* Plot lines
local lines_areas
foreach s in 2 1 3 {
  local line_count_`s' (connected coef xvar if spec==`s', yaxis(1) pstyle(p`s'))
  local ci_area_`s' (rarea ci_lower ci_upper xvar if spec==`s', sort pstyle(p`s'area))
  local lines_areas `lines_areas' `ci_area_`s'' `line_count_`s''
}

* Eligibility changes
local text_grid text(`=`y1_tick_lo' + 0.52*`y1_step'' 52.5 "Eligibility" "relaxes", place(c) just(center) box lwidth(none) bcolor(white%94))
local xlines_grid xline(50) xline(55)
local pci_grid (pci 7 50.6 6.9 50, pstyle(p5)) (pci 7 54.4 6.9 55, pstyle(p5))

local legend legend(order(4 2 6) label(2 "County (baseline)") label(4 "Constant") label(6 "Entry month") subtitle("Fixed effects (interacted with age bins)") rows(1))
local region graphregion(margin(t=5 b=-1))

twoway `lines_areas' `pci_grid', `x_axis' `y_axis1' `legend' `region' `text_grid' `xlines_grid' yline(0, lpattern(solid) lwidth(thin) lcolor(gs8))

* Export graph
cap mkdir "$SSDIMed/results/figures"
graph export "$SSDIMed/results/figures/entry_unempatapp_ageatentry-specs.pdf", as(pdf) fontface("Source Sans Pro") fontdir("$SSDIMed/data/raw/fonts") replace

}






** EOF
